package sales

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"errors"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"strconv"
)

/** 公司商品流向*/
func statistics(r *gin.RouterGroup) {
	statistics := r.Group("/statistics")
	{
		statistics.POST(sql.RequestAdd, statisticsAdd)
		statistics.POST(sql.RequestUpdate, statisticsUpdate)
		statistics.GET(sql.RequestData, statisticsData)
		statistics.GET(sql.RequestList, statisticsList)
		statistics.GET(sql.RequestDelete, statisticsDelete)
	}
}
func statisticsAdd(c *gin.Context) {
	sql.HttpHandle(sql.ADD, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var order model.SalesStatisticsOrder
		if err := c.BindJSON(&order); err != nil {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		db.Create(&order)

		db.Model(&model.Customer{}).
			Where(order.CustomerId).
			Update("product_str", "")

		if db.RowsAffected == 0 {
			if db.Error == nil {
				db.Error = errors.New(sql.ErrorService)
			}
			return db, nil
		}
		return db, order
	})
}

func statisticsUpdate(c *gin.Context) {
	sql.HttpHandle(sql.UPDATE, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.SalesStatisticsOrder
		if err := c.BindJSON(&bean); err != nil {
			db.Error = err
			return db, nil
		}
		db = db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&bean)
		if db.RowsAffected == 0 {
			if db.Error == nil {
				db.Error = errors.New(sql.ErrorService)
			}
			return db, nil
		}
		db.Save(&bean)
		return db, bean
	})
}

func statisticsData(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var bean model.SalesStatisticsOrder
		id := c.Query("id")
		db = db.
			Table("sales_statistics_order").
			Where("sales_statistics_order.id = ?", id).
			Preload("SalesStatisticsProduct", func(db *gorm.DB) *gorm.DB {
				db = db.Select("sales_statistics_product.*," +
					"product.factory_no,product.product_name,product.specification," +
					"customer.name").
					Joins("left join product ON product.id = sales_statistics_product.product_id").
					Joins("left join customer ON customer.id = sales_statistics_product.customer_id")
				return db
			}).
			First(&bean)
		if db.RowsAffected == 0 {
			return db, nil
		}
		return db, bean
	})
}

func statisticsList(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var list []model.SalesStatisticsOrder
		db.
			Preload("SalesStatisticsProduct", func(db *gorm.DB) *gorm.DB {
				db = db.Select("sales_statistics_product.*,product.factory_no,product.product_name,customer.name").
					Joins("left join product ON product.id = sales_statistics_product.product_id").
					Joins("left join customer ON customer.id = sales_statistics_product.customer_id")
				return db
			}).
			Find(&list)
		return db, list
	})
}

func statisticsDelete(c *gin.Context) {
	sql.HttpHandle(sql.DELETE, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		idStr := c.Query("id")
		id, err := strconv.Atoi(idStr)
		if err != nil {
			db.Error = errors.New(sql.ErrorParams)
			return db, nil
		}
		db = db.Select(clause.Associations).Delete(&model.SalesStatisticsOrder{
			Model: gorm.Model{ID: uint(id)},
		})
		return db, nil
	})
}
